04. Exercise: Inheritance

Exercise: Inheritance

Create a Person class

Task Description:

Previously, we created a Person class. Now we will do something similar—but we will also use inheritance and create subclasses of the Person class.

Task List:

Task Feedback:

Nice work!

ND079 C1 L3 A05 Person

Create the Student subclass

Task Description:

Next, you will be creating a subclass to the Person class named Student.

Task List:

Task Feedback:

Nice work!

ND079 C1 L3 A05 Student

Create the StudentEmployee subclass

Task Description:

Now, you will be creating a class to subclass the Student class named StudentEmployee

Task List:

Task Feedback:

Nice work!

ND079 C1 L3 A05 StudentEmployee

Create the PersonTester

Task Description:

Next, you will be creating a tester class named PersonTester to instantiate and execute the objects.

Task List:

Task Feedback:

Nice work!

ND079 C1 L3 A05 PersonTester

Solution

Notice the variables have the private access modifier to ensure they are not directly accessible, while the access modifiers for the methods are public.

public class Person {

    private String firstName;
    private String lasName;

    public Person(String firstName, String lasName) {
        super();
        this.firstName = firstName;
        this.lasName = lasName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLasName() {
        return lasName;
    }

    public void setLasName(String lasName) {
        this.lasName = lasName;
    }

    @Override
    public String toString() {
        return "Name:" + firstName + " " + lasName;
    }

}
public class Student extends Person {

    private String studentId;

    public Student(String firstName, String lasName, String studentId) {
        super(firstName, lasName);
        this.studentId = studentId;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    @Override
    public String toString() {
        return super.toString() + " student ID " + studentId;
    }
}
public class StudentEmployee extends Student {
    private String employeeId;
    private double rateOfPayPerHour;

    public StudentEmployee(String firstName, String lasName, String studentId, String employeeId,
            double rateOfPayPerHour) {
        super(firstName, lasName, studentId);
        this.employeeId = employeeId;
        this.rateOfPayPerHour = rateOfPayPerHour;
    }

    public String getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }

    public double getRateOfPayPerHour() {
        return rateOfPayPerHour;
    }

    public void setRateOfPayPerHour(double rateOfPayPerHour) {
        this.rateOfPayPerHour = rateOfPayPerHour;
    }

    @Override
    public String toString() {
        return super.toString() + " employee ID " + employeeId + " pay " + rateOfPayPerHour;
    }

}
public class PersonTester {

    public static void main(String[] args) {

        Person sally = new Person("Sally", "Phillips");
        System.out.println(sally);

        Student mike = new Student("Mike", "Thompson", "12345");
        System.out.println(mike);

        StudentEmployee jeff = new StudentEmployee("Jeff", "Sam", "4567", "#12345", 20.5);
        System.out.println(jeff);
    }

}